import { NodePool, AudioClip, macro, director, game, sys, v3, Vec3, AudioSource } from "cc";
import { AudioMgr } from "../game/UI/AudioMgr";
//一些公共的定义或者枚举

class GDef {
    strKey = "超级";
    TypeHuman = 1;//猎人
    TypeDino = 2;//恐龙
    SidePlayer = 1;//我方
    SideEnemy = 2;//敌方
    ArmStatAlive = 0;//正常
    ArmStatDead = 1;//死亡
    HumanCastTid = 1;//召唤猎人的Tid
    DinoCastTid = 1;//召唤恐龙的Tid
    prefabUI = {//预制体
    }
    wdEvent = {//注册事件
        resume: "resume",
        updateCoin: "updateCoin",
        addCoin: "addCoin",
        fightCoin: "fightCoin",
        playEffect: "playEffect",
        playBgm: "playBgm",
        //点击恐龙或猎人碰撞地图格子
        pressCell: "pressCell",
        //拖曳恐龙或猎人碰撞地图格子
        dragCell: "dragCell",
        //移动恐龙或猎人
        moveArm: "moveArm",
        //释放恐龙或猎人
        releaseArm: "releaseArm",
        //设置恐龙或猎人
        setArm: "setArm",
        //恐龙或猎人死亡
        armDie: "armDie",
        //子弹或恐龙击中
        attack: "attack",
        //胜利
        levelWin: "levelWin",
        //失败
        levelLose: "levelLose",
        //显示首页
        levelStart: "levelStart",
        //盲盒已開啓
        openManghe: "openManghe",
        //盲盒界面关闭
        closeManghe: "closeManghe",
        //召喚獵人
        castHuman: "castHuman",
        //召喚恐龙
        castDino: "castDino",
        //开始战斗
        startFight: "startFight",
        //显示结算
        showEnd: "showEnd",
        //过关
        levelUp: "levelUp",
        //弹窗
        showFrameArm: "showFrameArm",
        //增加一个蛋
        addEgg: "addEgg",
        //隐藏界面
        hideEggList: "hideEggList",
        //开始检测射线
        startRay: "startRay",
        //停止检测射线
        stopRay: "stopRay",
        addTeach: "addTeach",
        updateCastDino: "updateCastDino",
        updateCastHunter: "updateCastHunter",
        addCastDinoNum: "addCastDinoNum",//看广告增加召唤恐龙次数
        addCastHunterNum: "addCastHunterNum",//看广告增加召唤獵人次数
    }

    wxWords = [] //微信转发宣传语

    wxGridFlag = {
        //加载页矩阵广告
        loadLayerGrid: "loadLayerGrid",
        //游戏页上方横格子
        mainLayerTop: "mainLayerTop",
        //结算界面左右两侧格子
        rewardLeftGrid: "rewardLeftGrid",
        //结算界面左右两侧格子
        rewardRightGrid: "rewardRightGrid",
    }


    // gameObj = {

    // }
    //微信转发宣传图
    wxImg = [{
        url: "https://mmocgame.qpic.cn/wechatgame/N1GcdXLInXVZN67bg5LZBphEc0aaZpcPDIkqmhTYxv5cf90eXD9UCN2fHicAmf9zS/0",
        ID: "O7xBd9ILR1CduWeLEHnDXA=="
    }]

    //阵型
    teamShape = [
        [[2], [7], [12]],//1个人
        //2个人
        [
            [1, 3],
            [6, 8],
            [11, 13],
            [2, 7],
            [2, 12],
            [7, 12],
            [0, 4],
            [5, 9],
            [10, 14],
        ],
        //3个人
        [
            [1, 3, 7],
            [1, 3, 12],
            [0, 4, 7],
            [0, 4, 12],
            [6, 8, 12],
            [5, 9, 12],
            [2, 6, 8],
            [2, 5, 9],
            [2, 11, 13],
            [2, 10, 14],
            [7, 11, 13],
            [7, 10, 14],
            [1, 2, 3],
            [0, 2, 4],
            [6, 7, 8],
            [5, 7, 9],
            [11, 12, 13],
            [10, 12, 14],
        ],
        //4个人
        [
            [1, 2, 3, 7],
            [0, 2, 4, 7],
            [1, 2, 3, 12],
            [0, 2, 4, 12],
            [6, 7, 8, 12],
            [5, 7, 9, 12],
            [2, 6, 7, 8],
            [2, 11, 12, 13],
            [7, 11, 12, 13],
            [1, 3, 6, 8],
            [1, 3, 5, 9],
            [1, 3, 11, 13],
            [1, 3, 10, 14],
            [0, 4, 6, 8],
            [0, 4, 5, 9],
            [0, 4, 11, 13],
            [0, 4, 10, 14],
            [6, 8, 11, 13],
            [6, 8, 10, 14],
            [5, 9, 11, 13],
            [5, 9, 10, 14],
        ],
        //5个人
        [
            [1, 2, 3, 6, 8],
            [1, 2, 3, 5, 9],
            [1, 2, 3, 11, 13],
            [1, 2, 3, 10, 14],
            [1, 2, 3, 7, 12],
        ]
    ]
    //声音
    audioMgr: AudioSource = null;
    soundResouces: AudioClip[] = [];
    soundPlayTime: string[] = [];

    // private audio: boolean = true;//声音状态
    // private _filePath: String = 'AudioClip';
    // private soundResouces: AudioClip[] = [];//音频剪辑
    //预制体
    pack1 = "pack1"
    pack2 = "pack2"
    preCell = "prefab/preCell"
    preArm = "prefab/preArm"
    preHp = "prefab/preHp"
    preBullet = "prefab/preBullet"
    BulletsMgr = "prefab/BulletsMgr"
    preMain = "prefab/preMain"
    preCoinBar = "prefab/preCoinBar"
    preProgress = "prefab/preProgress"
    preEnd = "prefab/preEnd"
    preDamage = "prefab/preDamage"
    preJinbi = "spine/jinbi/preJinbi"
    preTail1 = "prefab/preTail1"
    preTail2 = "prefab/preTail2"
    prePiaodai = "spine/piaodai/prePiaodai"
    prePD = "spine/pd/prePD"
    preManghe = "spine/manghe/preManghe"
    preAttacked = "spine/gongji/gongji"
    // preSmoke = "spine/smoke/effectChangeCloth"
    preSmoke = "spine/hechengyanwu/hechengyanwu"
    preFrame = "prefab/preFrame"
    preHand = "spine/shouzhi/preHand"
    preContinue = "prefab/preContinue"
    preYilong = "prefab/preYilong"
    preAd = "prefab/preAd"
    preLajitong = "3Dmodel/lajitong/lajitong"
    preEgg = "3Dmodel/konglongdan/konglongdan"
    preEggList = "prefab/preEggList"
    preBtnEgg = "prefab/preBtnEgg"
    //场景预制体
    arrPackageChangjing = ["Changjing1", "Changjing2"]
    arrPreChangjing = ["changjing_1/changjing_1", "changjing_2/changjing_2"]
    //材质
    materialLightSrc = null;
    materialNormalSrc = null;
    textureLajitong = "3Dmodel/lajitong/texture";
    textureEgg = "3Dmodel/konglongdan/texture";
    //碰撞分组
    GroupPlayer = 1 << 1;
    GroupEnemy = 1 << 2;
    MaskPlayer = 1 << 1;
    MaskEnemy = 1 << 2;
    //UI_3D Layer
    LayerUI3D = 1 << 23;
    //格子位置
    cellPos = [];
    cellScale = 0.043;
    //通用移动速度
    moveSpeed = 0.01;
    //通用子弹速度
    moveSpeedBullet = 6;
    //通用攻击间隔
    delayAttack = 2;
    //角色体积缩放因子
    armScale = 0.02;
    //恐龙相对位置
    posOffsetDino = v3(0, 0, 0.8);
    //猎人相对位置
    posOffsetHuman = v3(0, 0, 0.8);
    //垃圾桶位置
    posLajitong = v3();
    //子弹数组
    arrBullet = [];
    nodeBullet = [];
    //对象池
    poolArm = new NodePool();
    poolArmModel = [];
    poolArmArrowModel = [];
    poolArmHp = new NodePool();
    poolDamage = new NodePool();
    poolCoin = new NodePool();
    poolAttacked = new NodePool();
    poolAD = new NodePool();
    poolEgg = new NodePool();
    poolBtnEgg = new NodePool();
    //特效
    nodeSmoke = null;
    nodePiaodai = null;
    nodePD = null;
    nodeManghe = null;
    nodeHand = null;
    nodeContinue = null;
    nodeYiLong = null;
    nodeAd = null;
    nodeLajitong = null;
    nodeEggList = null;
    //节点
    nodeRoot = null;
    nodeArms = null;
    Camera = null;
    CameraTop2D = null;
    CameraTop = null;
    //游戏状态
    intStat: number = 0;//0待机,1战斗,2结束
    eventListShowFrame = [];
    intRewardTime: number = 0;

    constructor() {
        let s = this.cellScale * 12;
        for (let i = 0; i < 30; i++) {
            let pos = v3(s * (i % 5) - s * 2, 0.01, i >= 15 ? (3.3 - Math.floor(i / 5) * s) : (-0.6 + Math.floor((i - 15) / 5) * s));
            this.cellPos.push(pos);
        }
        this.posLajitong = v3(s * (32 % 5) - s * 2, 0.01, (3.4 - Math.floor(32 / 5) * s));
    }

    getHumanPos(i) {
        if (i < 0) return null;
        return v3(this.cellPos[i].x + this.posOffsetHuman.x, this.cellPos[i].y + this.posOffsetHuman.y, this.cellPos[i].z + this.posOffsetHuman.z);
    }
    getDinoPos(i) {
        if (i < 0) return null;
        return v3(this.cellPos[i].x + this.posOffsetDino.x, this.cellPos[i].y + this.posOffsetDino.y, this.cellPos[i].z + this.posOffsetDino.z);
    }
    setRoot = function (node) {
        this.nodeRoot = node;
    };
    getRoot = function () {
        return this.nodeRoot;
    };
    setCamera = function (c) {
        this.Camera = c;
    };
    setCameraTop2D = function (c) {
        this.CameraTop2D = c;
    };
    setCameraTop2DWelcome = function (c) {
        this.CameraTop2DWelcome = c;
    };
    setCameraTop = function (c) {
        this.CameraTop = c;
    };
    getCamera = function () {
        return this.Camera;
    };
    getCameraTop2D = function () {
        return this.CameraTop2D;
    };
    getCameraTop2DWelcome = function () {
        return this.CameraTop2DWelcome;
    };
    getCameraTop = function () {
        return this.CameraTop;
    };
    //看激励视频
    showReward = function (cb) {
        if (cb) cb();
    }

    getDeviceBench(): number {
        if (!sys.isMobile) return 3;
        /* 获取屏幕尺寸 */
        let size = null;
        if (screen.windowSize) size = screen.windowSize;
        else size = screen;
        console.log("view size width:" + size.width + " height:" + size.height);
        console.log("sys.os:" + sys.os);
        if (sys.os == sys.OS.IOS) {
            if (size.height >= 1280) {
                /* 基本是pro系列 */
                return 4;
            }
            else if (size.height >= 1160) {
                /* iphone全面屏系列，除了x都还可以 */
                return 3;
            }
            else if (size.height >= 667) {
                return 2;
            }
            else {
                return 0;
            }

        } else {
            if (size.height >= 1400 && size.width > 3200) {
                /* 好屏幕配置一般不会太差 */
                return 4;
            }
            else if (size.height >= 1070 && size.width > 2300) {
                /* 比较新的全平面手机，GPU应该还可以 */
                return 3;
            }
            else if (size.height >= 1070 && size.width > 2260 && size.width <= 2300) {
                /* 是全面屏，但是屏幕比较老，配置应该一般 */
                return 2;
            }
            else if (size.height > 720) {
                /* 1080p的老机器 */
                return 1;
            }
            else {
                /* 1080以下的默认是渣渣 */
                return 0
            }
        }
    }

    setPerf(tier: number): void {
        console.log("device tier:" + tier);
        /* 定义全局性能Tier */
        /* get globals */
        let scene = director.getScene();
        let globals = null;
        if (scene) globals = scene.globals;
        /* 0挡以上开启天空盒 */
        // globals.skybox.enabled = (tier > 0) ? true : false;
        // /* 3挡以上开启IBL */
        // globals.skybox.useIBL = (tier > 3) ? true : false;
        // /* 0挡以上开雾效 */
        // globals.fog.enabled = (tier > 0) ? true : false;
        /* 2挡以上开阴影 */
        if (globals) globals.shadows.enabled = (tier > 1) ? true : false;
        /* 0挡以上开抗锯齿, 这个貌似无效哎 */
        macro.ENABLE_WEBGL_ANTIALIAS = (tier > 1) ? true : false;
        /* 2挡以上60帧，4档以下会降帧(可以参考Cocos源码，会跳帧) */
        game.frameRate = (tier > 0) ? 61 : 30;
        /* 2挡以上物理速度是1，其他是2 */
        // perfAdapter.Speed = (tier > 2) ? 1 : 2;
        // /* 2挡以上重力是-10，其他是-20(跳帧导致的) */
        // PhysicsSystem.instance.gravity.y = (tier > 2) ? -10 : -20;
        /* 3档以上ShadowMap Size设置为1024 */
        if (tier > 2) {
            if (globals) globals.shadows.shadowMapSize = (tier > 3) ? 1024 : 512;;
        }



        // hack, TODO: remove when engine fixed dirty flag 
        requestAnimationFrame(() => {
            if (director.root) director.root.pipeline.pipelineSceneData.shadows.shadowMapDirty = false;
        })

    }
}

export default new GDef;